<?php
/* Created by Chee Hwa
|  echo "{'success': true}" ;
|  $response_xml_data = new SimpleXMLElement($map_url);
|  echo print_r($response_xml_data);
|  https://libcat.tp.edu.sg/pds?func=authenticate&institute=TPL50&bor_id=1105204J&bor_verification=TB346833
|  $map_url = 'http://libcat.tp.edu.sg/X?op=bor-info&bor_id='.$userName;
*/
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class myaccount extends CI_Controller {
	public function doLogin() {
		$userName = $_POST['userName'];
		$userPassword = $_POST['userPassword'];
		$map_url = 'https://libcat.tp.edu.sg/pds?func=authenticate&institute=TPL50&bor_id='.$userName.'&bor_verification='.$userPassword;	
		
		$response_xml_data = simplexml_load_file($map_url);

		if($response_xml_data->children()->getName() == "auth")		
			$response = $response_xml_data->auth[0];
		else if($response_xml_data->children()->getName() == "error")
			$response = "N";
		else if($response_xml_data->children()->getName() == "name")
			$response = "E";
			
		echo $response;
		return $response;
	}
	
	public function getName() {
		$userName = $_POST['userName'];
		$map_url = 'http://libcat.tp.edu.sg/X?op=bor-info&bor_id='.$userName;
		$response = "";
		
		$response_xml_data = simplexml_load_file($map_url);	
		$response = (string) $response_xml_data->z303->{'z303-name'};
		
		echo $response;
		return $response;
	}
	
	public function getSession() {
		$userName = $_POST['userName'];
		$map_url = $this->config->item('account_details');
		$response = "";
		
		$response_xml_data = simplexml_load_file($map_url);	
		//$response = (string) $response_xml_data->z303->{'z303-name'};
		$response = (string) $response_xml_data->{'session-id'};
		
		echo $response;
		return $response;
	}
	
	public function getLoanNo() {
		$userName = $_POST['userName'];
		$map_url = 'http://libcat.tp.edu.sg/X?op=bor-info&bor_id='.$userName;
		$response = 1;
		
		$response_xml_data = simplexml_load_file($map_url);	
		$response = (int) $response_xml_data->z305->{'z305-no-loan'};
		
		echo $response;
		return $response;
	}
	
	public function getReservationNo() {
		$userName = $_POST['userName'];
		$map_url = 'http://libcat.tp.edu.sg/X?op=bor-info&bor_id='.$userName;
		$response = 0;
		
		$response_xml_data = simplexml_load_file($map_url);	
		$response = (int) $response_xml_data->z305->{'z305-no-hold'};
		
		echo $response;
		return $response;
	}
	
	public function getAllLoan() {
		$userName = $_POST['userName'];
		$map_url = 'http://libcat.tp.edu.sg/X?op=bor-info&bor_id='.$userName;
		/*
		|   first thing in array => $dueDate => z36-due-date
		|	second thing in array => $dueHour => z36-due-hour
		|	third thing in array => $title => z13-title
		|	forth thing in array => $renewal => z36-no-renewal
		|   Repeat: e.g. the first thing of the second part in array => $dueDate => z36-due-date
		*/
		$response = array();
		$noOfBook = 0;
		$response_xml_data = simplexml_load_file($map_url);	
		
		foreach ($response_xml_data->children() as $child)
			if($child->getName() == 'item-l') {
				array_push($response, array("dueDate"=>$child->z36->{'z36-due-date'},
				"dueHour"=>$child->z36->{'z36-due-hour'},
				"title"=>$child->z13->{'z13-title'},
				"renewal"=>$child->z36->{'z36-no-renewal'},
				// variables for renew
				"docNo"=>$child->z36->{'z36-doc-number'}, // <bib_no.>
				"itemSequence"=>$child->z36->{'z36-item-sequence'}, // <item_sequence>
				"id"=>$response_xml_data->z303->{'z303-id'}, // <bor_id>
				"itemStatus"=>$child->z30->{'z30-item-status'},
				$checkReservation = simplexml_load_file('http://libcat.tp.edu.sg/X?op=item-data&doc_number='.$child->z36->{'z36-doc-number'}.'&base=tpl01'),
				"onHold"=>$checkReservation->item->{'on-hold'}, // <on-hold>
				"requested"=>$checkReservation->item->{'requested'} // <requested>
				));
			}
			
		// echo print_r($response);	
		print_r(json_encode($response));
	}
	
	public function getAllReservation() {
		$userName = $_POST['userName'];
		$map_url = 'http://libcat.tp.edu.sg/X?op=bor-info&bor_id='.$userName;
		/*
		|   first thing in array => $title -> z13-title
		|	second thing in array => $status -> z37-status
		|	third thing in array => $requestDate -> 37-request-date
		|	forth thing in array => $endRequestDate => z37-end-request-date
		|   Repeat: e.g. the first thing of the second part in array => $title -> z13-title
		*/
		$response = array();
		$response_xml_data = simplexml_load_file($map_url);	
		
		foreach ($response_xml_data->children() as $child)
			if($child->getName() == 'item-h') {
				array_push($response, array("title"=>$child->z13->{'z13-title'},
				"status"=>$child->z37->{'z37-status'},
				"requestDate"=>$child->z37->{'z37-request-date'},
				"endRequestDate"=>$child->z37->{'z37-end-request-date'},
				// variables for cancel reservation
				"id"=>$response_xml_data->z303->{'z303-id'}, // <bor_id>
				"docNo"=>$child->z37->{'z37-doc-number'}, // <z37-doc-number>
				"itemSequence"=>$child->z37->{'z37-item-sequence'}, // <z37-item-sequence>
				"sequence"=>$child->z37->{'z37-sequence'}, // <z37-sequence>
				));
			}
			
		// echo print_r($response);	
		print_r(json_encode($response));		
	}

	public function doRenew() {
		$docNo = $_POST['docNo'];
		$itemSequence = $_POST['itemSequence'];
		$id = $_POST['id'];	
		$response = array();
		
		$map_url = 'http://libcat.tp.edu.sg/X?op=renew&doc_number='.$docNo.'&item_sequence='.$itemSequence.'&bor_id='.$id.'&library=tpl50';
		$response_xml_data = simplexml_load_file($map_url);
		
		if($response_xml_data->children()->getName() == "reply") {	
			array_push($response, array("dueDate"=>$response_xml_data->{'due-date'},
			"due-hour"=>$response_xml_data->{'due-hour'}));
		}
		else if($response_xml_data->children()->getName() == "error")
			array_push($response, array("error"=>$response_xml_data->{'error'}));
		
		print_r(json_encode($response));
	}
	
	public function cancelReservation() {
		$id = $_POST['id'];
		$docNo = $_POST['docNo'];
		$itemSequence = $_POST['itemSequence'];
		$sequence = $_POST['sequence'];
		$response = '';
		
		$map_url = 'http://libcat.tp.edu.sg/X?op=hold-req-cancel&library=tpl50&bor_id='.$id.'&doc_number='.$docNo.'&item_sequence='.$itemSequence.'&sequence='.$sequence;
		$response_xml_data = simplexml_load_file($map_url);
		
		if($response_xml_data->children()->getName() == "reply")		
			$response = "OK";
		else if($response_xml_data->children()->getName() == "error")
			$response = "NO";
		
		echo $response;
		return $response;
	}
}

?>